import React, { useCallback, useState } from 'react';
import { Button } from 'antd';

import { isSuccess } from '@/utils/request';

import { refreshFloatData } from '@/services/salaryManage/salaryCalculate';

import useEmployeeNoView from '@/pages/SalaryManage/hooks/useEmployeeNoView';

const EmployeeNoView = ({ updateStep, organizationCode, year, month }) => {
    const { render: renderEmployeeView, openModalFn } = useEmployeeNoView(organizationCode, year, month);
    const [loading, setLoading] = useState(false);

    const handle = useCallback(() => {
        setLoading(true);
        refreshFloatData({
            organizationCode,
            year,
            month
        })
            .then(res => {
                setLoading(false);
                if (isSuccess(res)) {
                    if (res.data.unViewed) {
                        openModalFn();
                    }
                    updateStep(res.data);
                }
            })
            .catch(() => {
                setLoading(false);
            });
    }, [month, openModalFn, organizationCode, updateStep, year]);

    return (
        <>
            <Button type="primary" ghost size="small" onClick={handle} loading={loading}>
                查看详情
            </Button>
            {renderEmployeeView()}
        </>
    );
};

export default EmployeeNoView;
